﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BL;
using DB;

namespace UI
{
    public partial class LessonsForm : Form
    {      

        public LessonsForm()
        {
            InitializeComponent();
            label1.Visible = false;
            employmentsdGVw.Visible = false;
            hourslabel.Text = TotalHours().ToString();
            ostatoklabel.Text = "0";
            employmentsdGVw.AllowDrop = true;
            employmentsdGVw.AllowUserToAddRows = true;
            employmentsdGVw.AllowUserToDeleteRows = true;
            this.employmentsdGVw.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.employmentsdGVw_CellValueChanged);
        }
       

        private void LessonsForm_Load(object sender, EventArgs e)
        {
            SubjectItem[] si = Subjects.Instance.Items;
            foreach (SubjectItem s in si)
                comboBox1.Items.Add(s.ToString());
        }   

        private void employmentsdGVw_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {      
            Topic t = new Topic();
            t.ID = Topiki.Instance.FreeID;
            t.Subject = Subjects.Instance[comboBox1.Text];
            Topiki.Instance.Add(t);
            TopicMapper tMapper = new TopicMapper();
            tMapper.Insert(t);

            employmentsdGVw.Rows[e.Row.Index - 1].Cells[0].Value = t.ID.ToString();
            AccessDb.Instance.Commit();
        }

        private void employmentsdGVw_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            Int64 id = Int64.Parse(employmentsdGVw.Rows[e.RowIndex].Cells[0].Value.ToString());
            Topic t = new Topic();
            t.ID = id;
            t.Subject = Subjects.Instance[comboBox1.Text];
            if (employmentsdGVw.Rows[e.RowIndex].Cells[1].Value != null)
                t.Date = employmentsdGVw.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (employmentsdGVw.Rows[e.RowIndex].Cells[2].Value != null)
                t.Theme = employmentsdGVw.Rows[e.RowIndex].Cells[2].Value.ToString();
            if (employmentsdGVw.Rows[e.RowIndex].Cells[3].Value != null)
            {
                t.Hours = int.Parse(employmentsdGVw.Rows[e.RowIndex].Cells[3].Value.ToString());
                hourslabel.Text = TotalHours().ToString();
                ostatoklabel.Text = OstatokHours(t.Subject.Hours).ToString();
            }
            Topiki.Instance[id] = t;
            TopicMapper tMapper = new TopicMapper();
            tMapper.UpdateEntity(t);
            AccessDb.Instance.Commit();
        }

        private void employmentsdGVw_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {            
            Topic t = Topiki.Instance[long.Parse(e.Row.Cells[0].Value.ToString())];

            TopicMapper tMapper = new TopicMapper();
            tMapper.DeleteEntity(t);

            Topiki.Instance.Delete(t.ID);
            hourslabel.Text = TotalHours().ToString();
            ostatoklabel.Text = OstatokHours(t.Subject.Hours).ToString();
            AccessDb.Instance.Commit();
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            employmentsdGVw.Visible = true;
            label1.Visible = true;
            employmentsdGVw.Rows.Clear();
            
            List<Topic> list = Topiki.Instance.SelectSubject(Subjects.Instance[comboBox1.Text].ID);
            foreach (Topic t in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells[0].Value = t.ID;
                row.Cells[1].Value = t.Date;
                row.Cells[2].Value = t.Theme;
                row.Cells[3].Value = t.Hours;
                employmentsdGVw.Rows.Add(row);
            }
            hourslabel.Text = TotalHours().ToString();
            ostatoklabel.Text = OstatokHours(Subjects.Instance[comboBox1.Text].Hours).ToString();
        }
        int TotalHours()
        {
            int totalHours = 0;
            foreach (DataGridViewRow dr in employmentsdGVw.Rows)
            {
                if (dr.Cells[3].Value != null)
                    totalHours += int.Parse(dr.Cells[3].Value.ToString());
            }
            return totalHours;
        }
        int OstatokHours(int total)
        {
            return total - int.Parse(hourslabel.Text);
        }
       
    }
}